// pages/orderList/orderList.js
const util = require("../../utils/util.js");
var app = getApp();
const requestManager = require("../../utils/request.js");
var router = require('../../utils/router.js');
const pageName = "PersonalCenter "
let orderId = ''
Page({

  /**
   * 页面的初始数据
   */
  data: { 
    orderIds:'',
    afterSaale: false, // 是否是售后服务订单
    serviceType: [], // 选择服务类型
    shipType: '', // 物流公司
    shipTypeNo: '', // 物流单号
    logisticsVisible: false, // 物流弹出框
    serviceVisible: false, // 退货弹出框
    // 用于iPhone X适配
    isIpx: app.globalData.isIpx ? true : false,
    orderList: [],
    status: 0, // 默认全部订单为0
    orderIndex: 0, // 默认选中哪里
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getOrderList(this.data.status)
    this.getServiceType()
  },
  // 获取售后列表
  afterSale(event) {
    this.getServiceList()
    this.setData({
      orderIndex: event.currentTarget.dataset.index,
      afterSaale: true
    })
  },
  handlelogisticsVisible(event) {
    if (event.currentTarget.dataset.shiptype) {
      this.setData({
        shipType: event.currentTarget.dataset.shiptype, // 物流公司
        shipTypeNo: event.currentTarget.dataset.shiptypeno // 物流单号
      })
    }
    this.setData({
      logisticsVisible: !this.data.logisticsVisible
    })
  },
  handleserviceVisible(event) {
    if(event){
      orderId = event.currentTarget.id 
    }
    this.setData({
      serviceVisible: !this.data.serviceVisible
    })
  },
  setOrderStatus(event) {
    this.getOrderList(event.currentTarget.id)
    this.setData({
      orderIndex: event.currentTarget.dataset.index,
      afterSaale: false
    })
  },
  goOrderInfo(event) { // 跳转订单详情
    let orderStatus = event.currentTarget.dataset.status
    if (this.data.afterSaale) {  // 如果是售后订单
      let params = {
        userId: util.getUserInfo().userId,
        orderId: event.currentTarget.id,
        serviceId:event.currentTarget.dataset.serviceid
      }
      requestManager.getDataResult(requestManager.service_view, params, "GET").then(res => {
        console.log('售后订单...',res.data)
        if(res.data.status==1){ // 退款中
          let url = "/pages/initiaterefund/initiaterefund"
          router.route.setAction(router.NAVIGATE_TO)
          .setParams([`serviceId=${params.serviceId}&orderId=${event.currentTarget.id}&afterSaale=${this.data.afterSaale}`])
          .setUrl(url)
          .run()
        }
        if(res.data.status==5){ // 审核未通过
          let url = "/pages/refundfail/refundfail";
          router.route.setAction(router.NAVIGATE_TO)
          .setParams([`serviceId=${params.serviceId}&orderId=${event.currentTarget.id}&afterSaale=${this.data.afterSaale}`])
          .setUrl(url)
          .run()
        }
        if(res.data.status==2 && res.data.refundShipView==0){ // 退货中 没有填写物流单号
          let url = "/pages/refund/refund";
          router.route.setAction(router.NAVIGATE_TO)
          .setParams([`orderId=${event.currentTarget.id}&afterSaale=${this.data.afterSaale}&serviceId=${params.serviceId}`])
          .setUrl(url)
          .run()
        }
        if(res.data.status==2 && res.data.refundShipView==1){ // 退货中 填写物流单号
          let url = "/pages/logisticsinfo/logisticsinfo";
          router.route.setAction(router.NAVIGATE_TO)
          .setParams([`orderId=${event.currentTarget.id}&afterSaale=${this.data.afterSaale}&serviceId=${params.serviceId}`])
          .setUrl(url)
          .run()
        }
        if(res.data.status==8){ // 退款成功
          let url = "/pages/refundsuccess/refundsuccess";
          router.route.setAction(router.NAVIGATE_TO)
          .setParams([`orderId=${event.currentTarget.id}&afterSaale=${this.data.afterSaale}&serviceId=${params.serviceId}`])
          .setUrl(url)
          .run()
        }
        if(res.data.status==17){ // 您撤消了本次退款申请
          let url = "/pages/cancelrefund/cancelrefund";
          router.route.setAction(router.NAVIGATE_TO)
          .setParams([`orderId=${event.currentTarget.id}&afterSaale=${this.data.afterSaale}&serviceId=${params.serviceId}`])
          .setUrl(url)
          .run()
        }
      })
    } else {
      if (orderStatus === 4 || orderStatus === 2) { // 4待发货 2已发货
        let url = "/pages/orderssecurity/orderssecurity";
        router.route.setAction(router.NAVIGATE_TO)
          .setParams([`orderid=${event.currentTarget.id}&afterSaale=${this.data.afterSaale}`])
          .setUrl(url)
          .run()
      }
    }
    // if(orderStatus===2){ // 已发货
    // }
    // console.log('orderId...',event.currentTarget.id)
  },
  // 提交单号
  fillLogistics(event){
    let url = "/pages/refund/refund";
        router.route.setAction(router.NAVIGATE_TO)
          .setParams([`orderid=${event.currentTarget.id}&afterSaale=${this.data.afterSaale}&serviceId=${event.currentTarget.dataset.serviceid}`])
          .setUrl(url)
          .run()
  },
  getOrderList(status) {
    wx.showLoading()
    // 获取商品列表
    let params = {
      userId: util.getUserInfo().userId,
      page: 0,
      status: status
    }
    // 订单列表
    requestManager.getDataResult(requestManager.goods_order_list, params, "GET").then(res => {
      console.log('订单列表接口数据...', res.list)
      this.setData({
        orderList: res.list ? res.list : [],
      })
      wx.hideLoading()
    })
  },
  // 获取售后列表
  getServiceList() {
    wx.showLoading()
    let params = {
      userId: util.getUserInfo().userId,
      page: 0
    }
    // 获取售后列表
    requestManager.getDataResult(requestManager.service_list, params, "GET").then(res => {
      console.log('获取售后列表...', res.list)
      this.setData({
        orderList: res.list ? res.list : [],
      })
      wx.hideLoading()
    })
  },
  handleCheckServer(event) {
    this.handleserviceVisible()
    let url = "/pages/reimburse/reimburse";
    router.route.setAction(router.NAVIGATE_TO)
      .setParams([`id=${event.currentTarget.id}&orderId=${orderId}`])
      .setUrl(url)
      .run()
  },
  // 获取售后服务类型
  getServiceType() {
    requestManager.getDataResult(requestManager.service_type, {}, "GET").then(res => {
      this.setData({
        serviceType: res.list ? res.list : [],
      })
    })
  },
  // 重新提交的逻辑（售后）
  resubmit(event){
    this.setData({
      orderIds:event.currentTarget.id 
    })
    this.selectComponent("#maskDelivery").handleserviceVisible();
  },
  // 取消申请(售后)
  withdraw(event){
    wx.showLoading()
    let params = {
      userId: util.getUserInfo().userId,
      orderId: event.currentTarget.id,
      serviceId:event.currentTarget.dataset.serviceid
    }
    // 取消售后列表
    requestManager.getDataResult(requestManager.order_service_revoke, params, "GET").then(res => {
      this.getServiceList()
    }).catch(err=>{
      wx.hideLoading()
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})